package jiang;
public class VolatileTest {
    volatile boolean b = false;
    int a = 0;
    public static void main(String[] args) {
        for (int i=0; i<10000000; i++) {
            VolatileTest resource = new VolatileTest();
            Thread t1 = new Thread(new A(resource));
            Thread t2 = new Thread(new B(resource));
            t1.start();
            t2.start();
        }
    }
}
class A implements Runnable {
    volatile VolatileTest r = null;
    public A(VolatileTest a) {
        r = a;
    }
    public void run() {
        r.a = 1;
        r.b = true;
    }
}
class B implements Runnable {
	volatile VolatileTest r = null;
    public B(VolatileTest a) {
        r = a;
    }
    public void run() {
        while (!r.b) {
            Thread.yield();
        }
        int temp = r.a;
        if (temp == 0) {
            System.out.println("出现CPU指令重排序");
        }
    }
}
